package com.molyfun.parents.activity.organization;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.OrganizationListResponse;
import com.molyfun.parents.view.TwoTabBar;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 */
public class OrganizationListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * TYPE_ADDED 已加入
     * TYPE_CHECK 审核中
     */
    private int mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
    private Context mContext;
    private int mOldChoosePosition = -1;
    private int mSelectedPosition = -1;

    private List<OrganizationListResponse.Organization> mOrganizationList;

    public OrganizationListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void refreshData(List<OrganizationListResponse.Organization> list, int tab){
        this.mOrganizationList = list;
        mCurrentTab = tab;
        if(tab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
            int position = 0;
            for(OrganizationListResponse.Organization organization : list){
                if(organization.isSelected()){
                    mOldChoosePosition = position;
                    mSelectedPosition = mOldChoosePosition;
                }
                position ++;
            }
        }
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(mOrganizationList != null){
            mOrganizationList.clear();
            notifyDataSetChanged();
        }
    }

    public OrganizationListResponse.Organization getChoosedOrganization(){
        if(mOldChoosePosition == mSelectedPosition){

//                return null;

        }
        if (mSelectedPosition < 0){
            return null;
        }
        return mOrganizationList.get(mSelectedPosition);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrganizationViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_organization_list_cell,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        OrganizationViewHolder organizationViewHolder = (OrganizationViewHolder) holder;
        final OrganizationListResponse.Organization organizationBean= mOrganizationList.get(position);
        organizationViewHolder.mOrganizationName.setText(organizationBean.getDepartname());
        organizationViewHolder.mStudentRemarkName.setText(organizationBean.getStudentremakname());
        if(mCurrentTab == TwoTabBar.TwoTabChooseTab.BAR_ONE){
            organizationViewHolder.mOrganizationCheckState.setVisibility(View.VISIBLE);
            organizationViewHolder.mOrganizationChecking.setVisibility(View.VISIBLE);
            organizationViewHolder.mOrganizationChecking.setText(organizationBean.getStatus());
            if(mSelectedPosition == position){
                organizationViewHolder.mOrganizationCheckState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);

            }else{
                organizationViewHolder.mOrganizationCheckState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
            }
//            organizationViewHolder.mOrganizationCheckState.setOnClickListener(new View.OnClickListener() {
            organizationViewHolder.mOrganizationCheckState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 切换选择的机构
                    mSelectedPosition = position;
                    notifyDataSetChanged();
//                    SignUpDetailActivity.startActivity(mContext, organizationBean.getDepartid(), organizationBean.getId());
                }
            });
            organizationViewHolder.mlist_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //TODO 切换选择的机构
                  if(!TextUtils.isEmpty(organizationBean.getId()) &&  !TextUtils.isEmpty(organizationBean.getEnrollid()) ) {
                      SignUpDetailActivity.startActivity(mContext, organizationBean.getId(), organizationBean.getEnrollid());
                  }else {
//                      UIUtils.showToast(mContext,"报名学生不存在");
                  }
                }
            });
            if (!TextUtils.isEmpty(organizationBean.getStatus()) && (organizationBean.getStatus().equals("通知入学") && organizationBean.getStatus().equals("已缴费"))) {
                //TODO 通知入学刷新数据
                if(organizationListCallBack != null) {
                    organizationListCallBack.refreshOrganizationListData();
                }
            }
        }/*else{
            organizationViewHolder.mOrganizationCheckState.setVisibility(View.GONE);
            organizationViewHolder.mOrganizationChecking.setVisibility(View.VISIBLE);
        }*/
    }

    @Override
    public int getItemCount() {
        if(mOrganizationList == null){
            return 0;
        }else{
            return mOrganizationList.size();
        }
    }
    public interface  OrganizationListCallBack {
        void  refreshOrganizationListData();
    }
    private  OrganizationListCallBack organizationListCallBack ;
    public void setOrganizationListListener( OrganizationListCallBack organizationListCallBack) {
        this.organizationListCallBack = organizationListCallBack;
    }
    class OrganizationViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.list_layout)
        RelativeLayout mlist_layout;
        @InjectView(R.id.organization_check_state)
        ImageView mOrganizationCheckState;
        @InjectView(R.id.organization_name)
        TextView mOrganizationName;
        @InjectView(R.id.organization_check)
        TextView mOrganizationChecking;
        @InjectView(R.id.student_remarkname)
        TextView mStudentRemarkName;

        public OrganizationViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
